package com.kady.factory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: kady
 * \* Date: 2021/3/24
 * \* Time: 19:11
 * \* 千峰JAVA2101班
 * \* Description: bean工厂类
 * \
 */
public class BeanFactory {
    private static Properties properties = new Properties();


    static{
        InputStream in = null;
        try {
             in = BeanFactory.class.getClassLoader().getResourceAsStream("com/kady/bean.properties");
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static Object getBean(String beanName)  {
        Object instance = null;
        String className = properties.getProperty(beanName);
        if(!className.isEmpty()){
            Class<?> clazz = null;
            try {
                clazz = Class.forName(className);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            try {
                instance = clazz.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return instance;
    }


}
